package com.oj.core.entity;

import com.oj.api.core.domain.JudgeInfo;
import com.oj.api.core.domain.JudgeLineInfo;
import com.oj.common.data.BaseEntity;
import lombok.*;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 评测实体
 * Created by Bing on 2018/5/4.
 */
@Entity(name = "CORE_JUDGE")
@Table(indexes = {
        @Index(name = "core_idx_code_judge", unique = true, columnList = "code"),
        @Index(name = "pms_idx_updatetime_judge", columnList = "UPDATE_TIME")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class JudgeEntity extends BaseEntity<JudgeInfo>{



    /**
     * 题目集编码
     * 题目编码
     * 代码
     * 编译器
     * 用户编码
     * 提示信息
     */
    protected String problemSetCode,problemCode,compile,userCode;


    @Column(length = 2000)
    protected String remark;

    @Column(length = 4000)
    protected String originCode;

    /**
     * 评分
     */
    protected Integer score;


    /**
     * 运行时间，运行内存
     */
    protected Long runTime,runMemory;


    /**
     * 评测详情集合
     */
    @OneToMany(mappedBy = "judgeEntity",cascade = CascadeType.ALL,fetch = FetchType.LAZY,orphanRemoval = true)
    @OrderBy(value = "name")
    private Set<JudgeLineEntity> lines;


    public void addLine(JudgeLineEntity judgeLineEntity){
        if(lines==null){
            lines = new HashSet<>();
        }
        judgeLineEntity.setJudgeEntity(this);
        lines.add(judgeLineEntity);
    }

    @Override
    protected JudgeInfo createInfo() {
        return new JudgeInfo();
    }

    @Override
    public JudgeInfo buildInfo(JudgeInfo judgeInfo) {
        super.buildInfo(judgeInfo);
        List<JudgeLineInfo> lineInfos = new ArrayList<>();
        if(this.lines!=null){
            this.lines.forEach(a->{
                lineInfos.add(a.buildInfo());
            });
        }
        judgeInfo.setLines(lineInfos);
        return judgeInfo;
    }
}
